package com.example.goup17booksmanagement;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ViewBookDetail extends Activity {
	// private Context mContext;
	private Button btnDelete, btnEdit, btnPickDate;
	private EditText edtName, edtAuthor, edtPublisher, edtPublishDate;
	private TextView tvName, tvAuthor, tvPublisher, tvPublishDate;
	private Context mContext;
	private LinearLayout lnPickDate;
	String index = "";
	String id = "";

	public static final int DATE_DIALOG_ID_2 = 1112;
	private int year = -1, month = -1, day = -1;
	String editDay = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_book_detail);

		mContext = this;

		// Get current Calendar for DatePicker
		Calendar temp = Calendar.getInstance();
		year = temp.get(Calendar.YEAR);
		month = temp.get(Calendar.MONTH);
		day = temp.get(Calendar.DAY_OF_MONTH);
		
		// UI Binding
		btnDelete = (Button) findViewById(R.id.btnDelete);
		btnEdit = (Button) findViewById(R.id.btnEdit);
		edtName = (EditText) findViewById(R.id.edtName);
		edtAuthor = (EditText) findViewById(R.id.edtAuthor);
		edtPublisher = (EditText) findViewById(R.id.edtPublisher);
		edtPublishDate = (EditText) findViewById(R.id.edtPublishDate);

		// TextView matching
		tvName = (TextView) findViewById(R.id.tvName);
		tvAuthor = (TextView) findViewById(R.id.tvAuthor);
		tvPublisher = (TextView) findViewById(R.id.tvPublisher);
		tvPublishDate = (TextView) findViewById(R.id.tvPublishDate);

		btnPickDate = (Button) findViewById(R.id.btnPickDate);
		lnPickDate = (LinearLayout) findViewById(R.id.lnPickDate);

		// Receiver Data
		Intent receiver = getIntent();

		String mode = receiver.getStringExtra("mode");
		if (mode.equals("view_mode")) {
			// Install ViewMode or Edit Mode
			installViewMode();

			id = receiver.getStringExtra("id");
			String name = receiver.getStringExtra("name");
			String author = receiver.getStringExtra("author");
			String publisher = receiver.getStringExtra("publisher");
			String publishDate = receiver.getStringExtra("publishdate");
			index = receiver.getStringExtra("index");

			// Show data
			tvName.setText(name);
			tvAuthor.setText(author);
			tvPublisher.setText(publisher);
			tvPublishDate.setText(publishDate);

			edtName.setText(name);
			edtAuthor.setText(author);
			edtPublisher.setText(publisher);
			edtPublishDate.setText(publishDate);
		}
		if (mode.equals("edit_mode")) {
			installEditMode();
			index = receiver.getStringExtra("index");
			tvName.setText("");
			tvAuthor.setText("");
			tvPublisher.setText("");
			tvPublishDate.setText("");

			edtName.setText("");
			edtAuthor.setText("");
			edtPublisher.setText("");
			edtPublishDate.setText("");
		}

		btnEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				installEditMode();
			}
		});

		btnDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (btnDelete.getText().toString().equals("Save")) {
					// Save all Data and ...
					installViewMode();

					tvName.setText(edtName.getText().toString());
					tvAuthor.setText(edtAuthor.getText().toString());
					tvPublisher.setText(edtPublisher.getText().toString());
					tvPublishDate.setText(edtPublishDate.getText().toString());

					Intent senderIntent = getIntent();
					senderIntent.putExtra("name", edtName.getText().toString());
					senderIntent.putExtra("index", index);
					senderIntent.putExtra("id", id);
					senderIntent.putExtra("author", edtAuthor.getText()
							.toString());
					senderIntent.putExtra("publisher", edtPublisher.getText()
							.toString());
					senderIntent.putExtra("publishdate", edtPublishDate
							.getText().toString());
					setResult(RESULT_CANCELED, senderIntent);
					// Creat new Object and Insert Into Database and ListView
				} else {
					DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							switch (which) {
							case DialogInterface.BUTTON_POSITIVE:
								// Do your Yes progress
								// perform delete
								Toast.makeText(mContext, "Delete ",
										Toast.LENGTH_SHORT).show();
								Intent senderIntent = getIntent();
								senderIntent.putExtra("name", "delete");
								senderIntent.putExtra("index", index);
								setResult(RESULT_CANCELED, senderIntent);
								finish();
								break;

							case DialogInterface.BUTTON_NEGATIVE:
								// Do your No progress
								break;
							}
						}
					};
					AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
					ab.setTitle("Xoa sach");
					ab.setMessage("Ban co muon xoa quyen sach nay?")
							.setPositiveButton("Yes", dialogClickListener)
							.setNegativeButton("No", dialogClickListener)
							.show();

				}
			}
		});

		btnPickDate.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(DATE_DIALOG_ID_2);
			}
		});
	}

	private void installViewMode() {
		btnDelete.setText("Delete");

		edtName.setVisibility(View.GONE);
		tvName.setVisibility(View.VISIBLE);

		edtAuthor.setVisibility(View.GONE);
		tvAuthor.setVisibility(View.VISIBLE);

		edtPublisher.setVisibility(View.GONE);
		tvPublisher.setVisibility(View.VISIBLE);

		edtPublishDate.setVisibility(View.GONE);
		tvPublishDate.setVisibility(View.VISIBLE);

		btnPickDate.setVisibility(View.GONE);
		lnPickDate.setVisibility(View.GONE);
	}

	private void installEditMode() {
		btnDelete.setText("Save");
		edtName.setVisibility(View.VISIBLE);
		tvName.setVisibility(View.GONE);

		edtAuthor.setVisibility(View.VISIBLE);
		tvAuthor.setVisibility(View.GONE);

		edtPublisher.setVisibility(View.VISIBLE);
		tvPublisher.setVisibility(View.GONE);

		edtPublishDate.setVisibility(View.VISIBLE);
		tvPublishDate.setVisibility(View.GONE);

		btnPickDate.setVisibility(View.VISIBLE);
		lnPickDate.setVisibility(View.VISIBLE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.view_book_detail, menu);
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID_2:
			// set date picker as current date
			return new DatePickerDialog(this, datePickerListener, year, month,
					day);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			year = selectedYear;
			month = selectedMonth;
			day = selectedDay;

			editDay = "" + day + "/" + month + "/" + year;
			// set selected date into datepicker also
			edtPublishDate.setText(editDay);
			tvPublishDate.setText(editDay);
		}
	};
}
